package monitoring;

import hifiSpeakers.TextBasedHifiLoud;
import hifiSpeakers.TextBasedHifiMedium;
import hifiSpeakers.TextBasedHifiQuiet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.TextArea;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import monitoring.application.AboutAction;
import monitoring.application.CreateClientAction;
import monitoring.application.QuitAction;
import monitoring.application.ShowDisplayAction;
import monitoring.application.ShowMonitorAction;
import monitoring.application.StartHifiAction;
import monitoring.application.StartServerAction;
import monitoring.application.StartStorageAction;
import monitoring.application.StopServerAction;
import server.Server;
import storage.Monitor;
import storage.TextBasedStorage;
import display.LeisDeadlyDisplay;

//import storage.Monitor;

/*
 * Author : Mohamed Delhomme
 */

public class UssProjectView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public Monitor monitor;
	public Server server;
    public LeisDeadlyDisplay display;
    public int idxClient = 0;
    
    public TextBasedStorage  storage;
    
    public String hifiType;
    public TextBasedHifiLoud   hifiLoud;
    public TextBasedHifiMedium hifiMedium;
    public TextBasedHifiQuiet  hifiQuiet;
    
	private JPanel mainPanel;

	private JMenuBar menuBar;
	private JMenu fileMenu;
	private JMenu helpMenu;
	public JMenuItem startServerItem;
	public JMenuItem stopServerItem;
	private JMenuItem aboutItem;
	private JMenuItem quitItem;

	public JButton buttonStartServer;
	public JButton buttonStopServer;
	public JLabel  labelStatusServer;
	
	public JButton buttonShowMonitor;
	public JButton buttonCreateClient;
	public JButton buttonShowDisplay;
	
	public JButton buttonStartStorage;
	public JButton buttonStartLoudHifi;
	public JButton buttonStartMediumHifi;
	public JButton buttonStartQuietHifi;
	
	public TextArea textServer;
	public TextArea textClient;
	public TextArea textMonitor;
	public TextArea textStorage;
	public TextArea textHifi;

	public UssProjectView() throws IOException {
		super();
		
		hifiLoud = new TextBasedHifiLoud();
		hifiMedium = new TextBasedHifiMedium();
		hifiQuiet = new TextBasedHifiQuiet();
		
		storage = new TextBasedStorage();

		build();
	}

	private void build() {

		// general properties for the main window
		setTitle("Uss Project Global Monitoring");
		setSize(600, 400);
		setLocationRelativeTo(null); // center the window on the screen
		setResizable(true); // avoiding resizing of the window
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		mainPanel = new JPanel();
		
		add(mainPanel);

		// #######################################################
		// Setting the menu bar
		// #######################################################

		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		helpMenu = new JMenu("Help");

		startServerItem = new JMenuItem(new StartServerAction(this, "Start Server"));
		stopServerItem = new JMenuItem(new StopServerAction(this, "Stop Server"));
		aboutItem = new JMenuItem(new AboutAction(this, "About Uss Project"));
		quitItem = new JMenuItem(new QuitAction("Quit"));
		
		stopServerItem.setEnabled(false);

		fileMenu.add(startServerItem);
		fileMenu.add(stopServerItem);
		fileMenu.addSeparator();
		fileMenu.add(quitItem);

		helpMenu.add(aboutItem);

		menuBar.add(fileMenu);
		menuBar.add(helpMenu);

		setJMenuBar(menuBar);

		// #######################################################
		// Setting the main panel
		// #######################################################

		mainPanel.setLayout(new BorderLayout());

		JTabbedPane tabMonitoring = new JTabbedPane();
		
		JPanel linkPanel = new JPanel();
		linkPanel.setLayout(new BoxLayout(linkPanel, BoxLayout.Y_AXIS));

		JPanel connectPanel = new JPanel(new BorderLayout());
		JPanel disconnectPanel = new JPanel(new BorderLayout());

		buttonStartServer = new JButton(new StartServerAction(this, "Start The Server"));
		buttonStartServer.setBackground(Color.white);
		
		buttonStopServer = new JButton(new StopServerAction(this, "Stop The Server"));
		buttonStopServer.setBackground(Color.white);
		buttonStopServer.setEnabled(false);

		connectPanel.add(buttonStartServer, BorderLayout.CENTER);
		disconnectPanel.add(buttonStopServer, BorderLayout.CENTER);

		linkPanel.add(connectPanel);
		linkPanel.add(disconnectPanel);

		labelStatusServer = new JLabel(
				"   Status : server currently off   ");

		mainPanel.add(tabMonitoring, BorderLayout.CENTER);
		mainPanel.add(linkPanel, BorderLayout.NORTH);
		mainPanel.add(labelStatusServer, BorderLayout.AFTER_LAST_LINE);
		

		// #######################################################
		// Setting the server tab
		// #######################################################
		
		JPanel serverPanel = new JPanel(new BorderLayout());
		tabMonitoring.add("Server", serverPanel);
		
		textServer = new TextArea();
		textServer.setEditable(false);
		textServer.setBackground(new Color(40, 40, 95));
		textServer.setForeground(new Color(0, 128, 255));
		
		serverPanel.add(textServer, BorderLayout.CENTER);
		
		// #######################################################
		// Setting the client tab
		// #######################################################
		
		JPanel clientPanel = new JPanel(new BorderLayout());
		tabMonitoring.add("Client", clientPanel);
		
		buttonCreateClient = new JButton(new CreateClientAction(this, "Create New Client"));
		buttonCreateClient.setBackground(Color.white);
		textClient = new TextArea();
		textClient.setEditable(false);
		textClient.setBackground(new Color(40, 40, 95));
		textClient.setForeground(new Color(0, 128, 255));
		
		clientPanel.add(buttonCreateClient, BorderLayout.NORTH);
		clientPanel.add(textClient, BorderLayout.CENTER);
		
		// #######################################################
		// Setting the monitoring tab
		// #######################################################
		
		JPanel monitoringPanel = new JPanel(new BorderLayout());
		tabMonitoring.add("Monitoring", monitoringPanel);
		
		buttonShowMonitor = new JButton(new ShowMonitorAction(this, "Show monitor on a separate window"));
		buttonShowMonitor.setBackground(Color.white);
		textMonitor = new TextArea();
		textMonitor.setEditable(false);
		textMonitor.setBackground(new Color(40, 40, 95));
		textMonitor.setForeground(new Color(0, 128, 255));
		
		monitoringPanel.add(buttonShowMonitor, BorderLayout.NORTH);
		monitoringPanel.add(textMonitor, BorderLayout.CENTER);
		
		// #######################################################
		// Setting the display tab
		// #######################################################
		
		JPanel displayPanel = new JPanel(new BorderLayout());
		tabMonitoring.add("Display", displayPanel);
		
		buttonShowDisplay = new JButton(new ShowDisplayAction(this, "Show the Display Window"));
		buttonShowDisplay.setBackground(Color.white);

		displayPanel.add(buttonShowDisplay, BorderLayout.NORTH);
		
		// #######################################################
		// Setting the storage tab
		// #######################################################
		
		JPanel storagePanel = new JPanel(new BorderLayout());
		tabMonitoring.add("Storage", storagePanel);
		
		buttonStartStorage = new JButton(new StartStorageAction(this, "Start the storage device"));
		buttonStartStorage.setBackground(Color.white);
		textStorage = new TextArea();
		textStorage.setEditable(false);
		textStorage.setBackground(new Color(40, 40, 95));
		textStorage.setForeground(new Color(0, 128, 255));
		
		storagePanel.add(buttonStartStorage, BorderLayout.NORTH);
		storagePanel.add(textStorage, BorderLayout.CENTER);
		
		// #######################################################
		// Setting the hi-fi tab
		// #######################################################

		JPanel hifiPanel = new JPanel(new BorderLayout());
		tabMonitoring.add("Hi-Fi", hifiPanel);
		
		buttonStartLoudHifi = new JButton(new StartHifiAction(this, "LOUD", "Start the loud hi-fi device"));
		buttonStartMediumHifi = new JButton(new StartHifiAction(this, "MEDIUM", "Start the medium hi-fi device"));
		buttonStartQuietHifi = new JButton(new StartHifiAction(this, "QUIET", "Start the quiet hi-fi device"));
		
		buttonStartLoudHifi.setBackground(Color.white);
		buttonStartMediumHifi.setBackground(Color.white);
		buttonStartQuietHifi.setBackground(Color.white);
		
		JPanel loudPanel = new JPanel(new BorderLayout());
		JPanel mediumPanel = new JPanel(new BorderLayout());
		JPanel quietPanel = new JPanel(new BorderLayout());
		
		loudPanel.add(buttonStartLoudHifi, BorderLayout.CENTER);
		mediumPanel.add(buttonStartMediumHifi, BorderLayout.CENTER);
		quietPanel.add(buttonStartQuietHifi, BorderLayout.CENTER);
		
		JPanel buttonHifiPanel = new JPanel();
		buttonHifiPanel.setLayout(new BoxLayout(buttonHifiPanel, BoxLayout.Y_AXIS));
		
		buttonHifiPanel.add(loudPanel);
		buttonHifiPanel.add(mediumPanel);
		buttonHifiPanel.add(quietPanel);
		
		textHifi = new TextArea();
		textHifi.setEditable(false);
		textHifi.setBackground(new Color(40, 40, 95));
		textHifi.setForeground(new Color(0, 128, 255));
		
		hifiPanel.add(buttonHifiPanel, BorderLayout.NORTH);
		hifiPanel.add(textHifi, BorderLayout.CENTER);
		
	} // build()

}
